﻿using System;
using System.IO;
using System.Net;
using System.Threading;

namespace UniRx
{
	// Token: 0x020001F4 RID: 500
	public static class WebRequestExtensions
	{
		// Token: 0x06000A7D RID: 2685 RVA: 0x00038160 File Offset: 0x00036560
		private static IObservable<TResult> AbortableDeferredAsyncRequest<TResult>(Func<AsyncCallback, object, IAsyncResult> begin, Func<IAsyncResult, TResult> end, WebRequest request)
		{
			//return Observable.Create<TResult>(delegate(IObserver<TResult> observer)
			//{
			//	//int isCompleted = -1;
			//	//IDisposable subscription = Observable.FromAsyncPattern<TResult>(begin, delegate(IAsyncResult ar)
			//	//{
			//	//	TResult result;
			//		//try
			//	//	{
			//	//		Interlocked.Increment(ref isCompleted);
			//	//		result = end(ar);
			//	//	}
			//	//	catch (WebException ex)
			//	//	{
			//	//		if (ex.Status != WebExceptionStatus.RequestCanceled)
			//	//		{
			//	//			throw;
			//	//		}
			//	//		result = default(!0);
			//	//	}
			//	//	return result;
			//	//})().Subscribe(observer);
			//	//return Disposable.Create(delegate
			//	//{
			//	//	if (Interlocked.Increment(ref isCompleted) == 0)
			//	//	{
			//	//		subscription.Dispose();
			//	//		request.Abort();
			//	//	}
			//	//});
			//});
			return null;
		}

		// Token: 0x06000A7E RID: 2686 RVA: 0x0003819B File Offset: 0x0003659B
		public static IObservable<WebResponse> GetResponseAsObservable(this WebRequest request)
		{
			return WebRequestExtensions.AbortableDeferredAsyncRequest<WebResponse>(new Func<AsyncCallback, object, IAsyncResult>(request.BeginGetResponse), new Func<IAsyncResult, WebResponse>(request.EndGetResponse), request);
		}

		// Token: 0x06000A7F RID: 2687 RVA: 0x000381C0 File Offset: 0x000365C0
		public static IObservable<HttpWebResponse> GetResponseAsObservable(this HttpWebRequest request)
		{
			return WebRequestExtensions.AbortableDeferredAsyncRequest<HttpWebResponse>(new Func<AsyncCallback, object, IAsyncResult>(request.BeginGetResponse), (IAsyncResult ar) => (HttpWebResponse)request.EndGetResponse(ar), request);
		}

		// Token: 0x06000A80 RID: 2688 RVA: 0x00038203 File Offset: 0x00036603
		public static IObservable<Stream> GetRequestStreamAsObservable(this WebRequest request)
		{
			return WebRequestExtensions.AbortableDeferredAsyncRequest<Stream>(new Func<AsyncCallback, object, IAsyncResult>(request.BeginGetRequestStream), new Func<IAsyncResult, Stream>(request.EndGetRequestStream), request);
		}
	}
}
